<template>
  <div class="article-container">
    <el-card class="box-card">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 主体表格 -->
      <el-table :data="comments" style="width: 100%">
        <el-table-column prop="title" label="文章标题"> </el-table-column>
        <el-table-column prop="total_comment_count" label="总评论数">
        </el-table-column>

        <el-table-column prop="fans_comment_count" label="粉丝评论数">
        </el-table-column>
        <el-table-column label="评论状态">
          <template slot-scope="scope">
            {{ formatStatus(scope.row.comment_status) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <!--
            如果前面是正常显示关闭评论按钮
            如果前面是关闭显示打开评论按钮
           -->
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              v-if="scope.row.comment_status === true"
              @click="toggleComment(scope.row, !scope.row.comment_status)"
              >关闭评论</el-button
            >
            <el-button
              size="mini"
              type="danger"
              v-if="scope.row.comment_status === false"
              @click="toggleComment(scope.row, !scope.row.comment_status)"
              >打开评论</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 10px"
        background
        layout="prev, pager, next"
        @current-change="pageChange"
        :page-size="10"
        :total="total_count"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
import { toggleCommentStatus } from '@/api/comment'

export default {
  name: 'CommentIndex',
  data () {
    return {
      total_count: 0,
      comments: [], // 评论数据
      pageSize: 10, // 每页多少条数据
      curPage: 1 // 当前请求的是第几页
    }
  },
  methods: {
    // 打开评论
    toggleComment (row, allowComment) {
      const params = {
        article_id: row.id.toString()
      }
      // data代表body（request body） 在请求体里
      const data = {
        allow_comment: allowComment
      }
      toggleCommentStatus(params, data).then(res => {
        // 成功之后刷新列表 更新UI显示
        // this.hGetComment()
        row.comment_status = allowComment
      })
    },
    pageChange (pageNum) {
      // pageNum 代表当前点击的是第几页
      this.curPage = pageNum
      this.hGetComment()
    },
    // 获取评论数据
    hGetComment () {
      const params = {
        page: this.curPage, // 页数
        per_page: this.pageSize, // 每页数量
        response_type: 'comment' // 用于获取评论管理的专用字段
      }
      getArticles(params).then(res => {
        this.comments = res.data.data.results
        this.total_count = res.data.data.total_count
      })
    },
    formatStatus (status) {
      const statusObj = {
        true: '正常',
        false: '关闭'
      }
      return statusObj[status]
    }
  },
  created () {
    this.hGetComment()
  }
}
</script>
